package org.atlantis.mina.demo1;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

/**
 * 一个简单的Mina服务器。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">李曦</a>
 * 
 */
public class SimpleServer {

	private final static int PORT = 8888;

	public static void main(String[] args) throws IOException {
		// 创建一个非阻塞的Server端Socket，使用NIO
		SocketAcceptor acceptor = new NioSocketAcceptor();
		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
		// 设定这个过滤器一行一行（/r/n）的读取数据
		chain.addLast("simple-chain", new ProtocolCodecFilter(
				new TextLineCodecFactory()));
		// 设定服务器端的消息处理器
		acceptor.setHandler(new SimpleMinaServerHandler());
		// 绑定端口，启动服务器
		acceptor.bind(new InetSocketAddress(PORT));
		System.out.println("Mina服务器启动，监听端口：" + PORT);
	}
}
